package com.heima.article.task;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.heima.common.minio.service.MinIOService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;

/**
 * 生成文章静态页html
 *
 * @author: itheima
 * @create: 2021-12-26 11:21
 */
@Slf4j
@Component
public class ArticleTask {


    @Autowired
    private IApArticleContentService articleContentService;

    /**
     * freemarker.template; 一定到导入freemarker
     */
    @Autowired
    private Configuration configuration;

    @Autowired
    private MinIOService minIOService;

    @Autowired
    private IApArticleService articleService;

    /**
     * 创建某一篇文章对应HTML文件，将文件上传到MinIO中得到文件访问地址
     *
     * @param articleId
     * @throws IOException
     * @throws TemplateException
     */
    @Async
    public void createArticleHtml(Long articleId) {
        try {
            //1.根据文档ID查询文章内容
            LambdaQueryWrapper<ApArticleContent> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApArticleContent::getArticleId, articleId);
            ApArticleContent content = articleContentService.getOne(queryWrapper);

            //2.基于Freemarker技术生成静态HTML文件
            //2.1 加载模板文件 templates/article.ftl  默认找templates目录模板文件
            Template template = configuration.getTemplate("article.ftl");

            //2.2 定义数据模型
            HashMap<String, Object> map = new HashMap<>();
            map.put("content", JSON.parseArray(content.getContent()));

            //2.3 使用模板引擎将模板跟数据进行合并
            StringWriter writer = new StringWriter();
            template.process(map, writer);

            //3.将生成静态页HTML上传到MinIO中
            InputStream inputStream = new ByteArrayInputStream(writer.toString().getBytes());
            String url = minIOService.upload(articleId + ".html", inputStream, "text/html");

            //4.将得到静态化url地址更新到文章表ap_article中 静态页字段
            LambdaUpdateWrapper<ApArticle> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ApArticle::getId, articleId);
            updateWrapper.set(ApArticle::getStaticUrl, url);
            articleService.update(updateWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("生成静态页失败；{}", e);
        }
    }

}
